<template>
  <div class="code-block" :class="{ 'is-collapsed': isCollapsed && collapsible }">
    <!-- 代码头部 -->
    <div class="code-header">
      <!-- 语言标识 -->
      <div class="language-label" v-if="language">{{ language }}</div>
      
      <!-- 操作按钮 -->
      <div class="actions">
        <el-tooltip content="复制代码" placement="top" :show-after="300">
          <el-button 
            class="action-btn" 
            type="text" 
            size="small"
            @click="copyCode"
          >
            <el-icon :size="14"><CopyDocument /></el-icon>
          </el-button>
        </el-tooltip>
        
        <el-tooltip 
          :content="isCollapsed ? '展开代码' : '折叠代码'" 
          placement="top" 
          :show-after="300"
          v-if="collapsible"
        >
          <el-button 
            class="action-btn" 
            type="text" 
            size="small"
            @click="toggleCollapse"
          >
            <el-icon :size="14">
              <ArrowDown v-if="isCollapsed" />
              <ArrowUp v-else />
            </el-icon>
          </el-button>
        </el-tooltip>
      </div>
    </div>
    
    <!-- 代码内容 -->
    <div class="code-content" :style="contentStyle">
      <pre><code ref="codeEl" :class="codeClass">{{ code }}</code></pre>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue';
import { CopyDocument, ArrowDown, ArrowUp } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import hljs from 'highlight.js';
import 'highlight.js/styles/atom-one-dark.css';

const props = defineProps({
  code: {
    type: String,
    required: true
  },
  language: {
    type: String,
    default: ''
  },
  collapsible: {
    type: Boolean,
    default: true
  },
  maxHeight: {
    type: Number,
    default: 400
  }
});

const isCollapsed = ref(false);
const codeEl = ref(null);

// 根据语言和代码长度判断是否可折叠
const shouldBeCollapsible = computed(() => {
  return props.collapsible && props.code.split('\n').length > 15;
});

// 设置样式
const contentStyle = computed(() => {
  if (isCollapsed.value && shouldBeCollapsible.value) {
    return { maxHeight: '150px' };
  }
  return { maxHeight: props.maxHeight + 'px' };
});

// 语言类名
const codeClass = computed(() => {
  return props.language ? `language-${props.language}` : '';
});

// 复制代码
const copyCode = () => {
  navigator.clipboard.writeText(props.code)
    .then(() => {
      ElMessage.success('代码已复制到剪贴板');
    })
    .catch(() => {
      ElMessage.error('复制失败');
    });
};

// 切换折叠状态
const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value;
};

// 高亮显示
const highlightCode = () => {
  if (codeEl.value) {
    hljs.highlightElement(codeEl.value);
  }
};

// 监听代码变化重新高亮
watch(() => props.code, () => {
  nextTick(() => {
    highlightCode();
  });
});

onMounted(() => {
  highlightCode();
  
  // 默认长代码折叠
  if (shouldBeCollapsible.value) {
    isCollapsed.value = true;
  }
});
</script>

<style scoped>
.code-block {
  position: relative;
  margin: 16px 0;
  border-radius: 6px;
  overflow: hidden;
  background-color: #282c34;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
}

.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  background-color: #21252b;
  color: #abb2bf;
  font-size: 12px;
}

.language-label {
  font-weight: 500;
  text-transform: uppercase;
}

.actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  color: #abb2bf !important;
}

.action-btn:hover {
  color: #fff !important;
}

.code-content {
  overflow-x: auto;
  overflow-y: auto;
  position: relative;
  transition: max-height 0.3s ease;
}

.is-collapsed .code-content::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: linear-gradient(to bottom, rgba(40, 44, 52, 0), rgba(40, 44, 52, 1));
  pointer-events: none;
}

.code-content pre {
  margin: 0;
  padding: 16px;
}

.code-content code {
  font-family: inherit;
  tab-size: 2;
}
</style> 